<template>
  <a-modal v-model:open="isOpen" title="下载报告" :footer="null">
    <div class="report-download">
      <div class="choose-box">
        <div
          class="choose-item"
          :class="{ cur: curIndex == 0 }"
          @click="handleChoose(0)"
        >
          <img class="icon" src="@/assets/images/down-pdf.png" />
          <div class="choose-text">PDF文件</div>
        </div>
        <div
          class="choose-item"
          :class="{ cur: curIndex == 1 }"
          @click="handleChoose(1)"
        >
          <img class="icon" src="@/assets/images/down-word.png" />
          <div class="choose-text">DOCX文件</div>
        </div>
      </div>
      <a-button type="primary" @click="handleFinish">确定下载</a-button>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, computed } from "vue";
import { message } from "ant-design-vue";
const props = defineProps({
  value: Boolean,
  modelValue: Boolean,
  id: String,
});
const curIndex = ref(null);
const handleChoose = (index) => {
  curIndex.value = index;
};
const isOpen = computed({
  get() {
    return props.value || props.modelValue;
  },
  set(val) {
    emit("update:value", val);
  },
});
const emit = defineEmits(["update:value"]);
function handleFinish() {
  if (curIndex.value === null) {
    message.error("请选择下载格式");
    return;
  }
  let url = "";
  console.log(props);
  if (curIndex.value === 0) {
    url = `${
      import.meta.env.VITE_API_URL
    }/api/StudentExperiment/PdfForStudentExperiment?student_experiment_id=${
      props.id
    }`;
  }
  if (curIndex.value === 1) {
    url = `${
      import.meta.env.VITE_API_URL
    }/api/StudentExperiment/WordForStudentExperiment?student_experiment_id=${
      props.id
    }`;
  }

  //下载文件
  window.open(url);

  isOpen.value = false;
}
</script>
<style lang="less" scoped>
.report-download {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  .choose-box {
    display: flex;
    align-items: center;
    padding: 16px 0;
    justify-content: center;
    .choose-item {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      box-sizing: border-box;
      cursor: pointer;
      width: 210px;
      margin: 0 15px;
      height: 210px;
      background: #ffffff;
      border-radius: 12px;
      border: 1px solid #e6ebf1;
      position: relative;
      &.cur {
        width: 210px;
        height: 210px;
        background: #ffffff;
        border-radius: 12px;
        border: 2px solid #2776ff;
        &::after {
          content: "";
          position: absolute;
          right: 0;
          top: 0;
          width: 20px;
          height: 20px;
          background: url(@/assets/images/down-checked.png) no-repeat;
          background-size: 100%;
        }
      }
      .icon {
        width: 124px;
        height: 140px;
        margin-bottom: 24px;
      }
      .choose-text {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 14px;
        color: #222222;
        line-height: 20px;
        text-align: right;
        font-style: normal;
      }
    }
  }
}
</style>
